#include "StdAfx.h"
#include "DDefaultLineCap.h"
#include "stdio.h"

#include "../ImagePro/ImageTIF.h"


DDefaultLineCap::DDefaultLineCap(void)
{
	m_nRotation = 0;
}

DDefaultLineCap::~DDefaultLineCap(void)
{
}


int DDefaultLineCap::Initialize()
{
	PlugReg::m_xRegParam.dwReg = 1;
	sprintf(PlugReg::m_xRegParam.szMachineCode, "XE3PA11M2XSRBSVHKFCROLJP7GYE8UUMF3Y56SCR");
	sprintf(PlugReg::m_xRegParam.szRegisterCode,"4094MCB04OO60SO106OC00UOII4RCR390U990ZUM");


	char szModulePath[MAX_PATH] = "";
	char szTestFile[MAX_PATH] = "";

	GetModuleFileName(NULL,szModulePath,MAX_PATH);
	char* sTemp = strrchr(szModulePath,'\\'); 
	sTemp++;
	sTemp[0] = '\0'; 

	char sExtFile[2][5] = {"tif","tiff"};
	int nItem = -1;
	while( ++nItem < 2 )
	{
		sprintf(szTestFile,"%stest.%s",szModulePath,sExtFile[nItem] );
		if( ::GetFileAttributes(szTestFile) != 0xffffffff )
		{
			break;
		}
	}

	if( nItem >= 2 )
	{
		return -1;
	}

	m_ImgBitInfo.nImgDefine   = eImage_Define_Img16;
	m_ImgBitInfo.pImageHeader = &m_x16BitInfo;


	int nBitsPerSample;

	CImageTIF xTif;
    xTif.GetImgBitPerSap(szTestFile,nBitsPerSample);
	if( nBitsPerSample < 9 || nBitsPerSample > 16)
	{
		return -1;
	}

	xTif.OpenImgFile(szTestFile,&m_ImgBitInfo);

	m_nReadLines = 0;

	m_nRotation = eCapRot_Mirror;

// 	m_xImagePro.Initialize();
// 	m_xImagePro.LoadFile( szTestFile );
// 
// 	zkImgBitInfo *pBitInfo = NULL;
// 	int nRet = m_xImagePro.GetImgBuffer(&pBitInfo);
// 	if( DD_ERR_SUCCESS != nRet )
// 	{
// 		return nRet;
// 	}
// 
// 	if( NULL != pBitInfo )
// 	{ 
// 		if( NULL != pBitInfo->pImgCapParam )
// 		{
// 			memcpy( pBitInfo->pImgCapParam,&m_xImgCapParma,sizeof(m_xImgCapParma) );
// 		}
// 		if( NULL != pBitInfo->pImgAssParam )
// 		{
// 			memset( pBitInfo->pImgAssParam,0,sizeof(zkImgAssParam) );
// 		}
// 	}


	return DD_ERR_SUCCESS;
}

int DDefaultLineCap::GetImgDefine(int &nImgDefine)
{
	nImgDefine = eImage_Define_Img16;
	return DD_ERR_SUCCESS;
}
int DDefaultLineCap::GetImgBitPerSap(int &nBitPerSample)
{
	nBitPerSample = 16;
	return DD_ERR_SUCCESS;
}
int DDefaultLineCap::GetImgSapPerPixel(int &nSapPerPixel)
{
	nSapPerPixel = 1;
	return DD_ERR_SUCCESS;
}

#define CapLineTime_Default 5

int DDefaultLineCap::GetImgCapLineTime(int &nCapLineTime)
{
	nCapLineTime = CapLineTime_Default;
	return 0;
}


int DDefaultLineCap::GetPlugDescription( zkCapDevDesc *pDescs,int &nCount )
{

	return DD_ERR_SUCCESS;
}


int DDefaultLineCap::SelectCap( zkCapDevDesc *pDesc )
{
	return DD_ERR_FAILURE;
}



#define DKQ_CHECK_ACTIVE  //if( NULL == m_pXtkDev ){return DD_ERR_NOTOPENDEV;}

int DDefaultLineCap::StartCapture()
{
	DKQ_CHECK_ACTIVE;
	return DD_ERR_SUCCESS;
}


int DDefaultLineCap::StopCapture()
{
	DKQ_CHECK_ACTIVE;
	return DD_ERR_SUCCESS;
}

int DDefaultLineCap::GetLinePixelCount(int &nPixelCount)
{
	DKQ_CHECK_ACTIVE;
	nPixelCount = m_x16BitInfo.nWidth;

	return DD_ERR_SUCCESS;
}

int DDefaultLineCap::GetLinePixel(void *pData)
{
	DKQ_CHECK_ACTIVE;

	Sleep(CapLineTime_Default);

	if( m_nRotation & eCapRot_Mirror )
	{
		int   nWidth   = m_x16BitInfo.nWidth;
		WORD *pSrc = m_x16BitInfo.pwImageData + m_nReadLines * nWidth;
        WORD *pDst = (WORD*)pData;
		for(int i=0;i<nWidth;i++)
		{
			pDst[i] = pSrc[nWidth-i-1];
		}
	}
	else
	{
		int   nSize    = m_x16BitInfo.nWidth*sizeof(WORD);
		int   nReadPos = m_nReadLines * m_x16BitInfo.nWidth;
		WORD *p = m_x16BitInfo.pwImageData;
		memcpy( pData,p+nReadPos,nSize );
	}

	m_nReadLines++;

	if( m_nReadLines >= m_x16BitInfo.nHeight )
	{
		m_nReadLines = 0;
	}
	
	return DD_ERR_SUCCESS;
}


int DDefaultLineCap::OpCapConfig(DWORD dwCmd,void *pVoid,DWORD dwInSize,DWORD *pdwOutSize)
{
	int nRet = DD_ERR_FAILURE;

	zkFCaptureParam *pFCapParam = (zkFCaptureParam*)pVoid;

	switch( dwCmd )
	{
	case eCap_Parma_ImageViewRect:
		{
			if( NULL == pVoid )
			{
				nRet = DD_ERR_PARAMETER;
			}
			else
			{
				RECT *pTemp = (RECT *)pVoid; 

				nRet = DD_ERR_SUCCESS;
			}
		}
		break;
	case eCap_Parma_GetRegisterInfo:
		{
			if( NULL == pVoid || NULL = pdwOutSize || dwInSize < sizeof(zkPlugRegParam) )
			{
				nRet = DD_ERR_PARAMETER;
			}
			else
			{
				memcpy( pVoid,&(PlugReg::m_xRegParam),sizeof(zkPlugRegParam) );
				*pdwOutSize = sizeof(zkPlugRegParam);

				nRet = DD_ERR_SUCCESS;
			}
		}
		break;
	case eCap_Parma_SetRegisterInfo:
		{
		}
		break;
	case eCap_Parma_GetRevise_Param:
		{
			if( NULL == pVoid || NULL = pdwOutSize || dwInSize < sizeof(zkCameraParam) )
			{
				nRet = DD_ERR_PARAMETER;
			}
			else
			{
				( (zkCameraParam*)pVoid )->fGain          = 0.25f;
				( (zkCameraParam*)pVoid )->fFrameSpeed    = 100.0f;
				( (zkCameraParam*)pVoid )->nPixelCombined = 1;    

				*pdwOutSize = sizeof(zkCameraParam);

				nRet = DD_ERR_SUCCESS;
			} 
		}
		break;
	case eCap_Parma_GetRevise_Plus: 
		{

		}
		break;
	case eCap_Parma_GetReviseList_Plus:
		{
           float *fPlus = (float*)m_szTemp;

		   fPlus[0] = 1;
		   fPlus[1] = 2;
		   fPlus[2] = 5;
		   fPlus[3] = 10;
		   fPlus[4] = 20;
		   fPlus[5] = 50;
		   fPlus[6] = 100;
		   fPlus[7] = 200;

		   pFCapParam->bAutoDel   = FALSE;
		   pFCapParam->nParamSize = 8 * sizeof(float);
		   pFCapParam->pParam     = (void*)m_szTemp;
		}
		break;
	case eCap_Parma_SetRevise_Plus:  
		{

		}
		break;
	case eCap_Parma_GetRevise_FrameSpeed:    
		{
			
		}
		break;
	case eCap_Parma_GetReviseList_FrameSpeed:  
		{
			float *fFrameSpeed = (float*)m_szTemp;

			fFrameSpeed[0] = 60;
			fFrameSpeed[1] = 80;
			fFrameSpeed[2] = 100;
			fFrameSpeed[3] = 150;
			fFrameSpeed[4] = 200;

			pFCapParam->bAutoDel   = FALSE;
			pFCapParam->nParamSize = 5 * sizeof(float);
			pFCapParam->pParam     = (void*)m_szTemp;
		}
		break;
	case eCap_Parma_SetRevise_FrameSpeed:  
		{

		}
		break;
	case eCap_Parma_GetRevise_PixelCombined: 
		{

		}
		break;
	case eCap_Parma_GetReviseList_PixelCombined:
		{
			int *nPixelCombined = (int*)m_szTemp;

			nPixelCombined[0] = 1;
			nPixelCombined[1] = 2;
			nPixelCombined[2] = 4;

			pFCapParam->bAutoDel   = FALSE;
			pFCapParam->nParamSize = 3 * sizeof(int);
			pFCapParam->pParam     = (void*)m_szTemp;
		}
		break;
	case eCap_Parma_SetRevise_PixelCombined:  
		{

		}
		break;
	case eCap_Parma_Pixel21:
		{
			
		}
		break;
	case eCap_Parma_GetCapNetInfo:
		{
			
		}
		break;
	case eCap_Parma_SetCapNetInfo:
		{

		}
		break;
	default:
		nRet = DD_ERR_FAILURE;
		break;
	}
	
	return nRet;
}



